using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using MySql.Data.MySqlClient;
using LdExpress;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace LdExpress
{
    /// <summary>
    /// Region
    /// </summary>
    public class Region
    {
        public long ID = 0;
        public string Name = "";
        public string Code = "";
        public int Sort = 0;
        public Region()
        {
        }
        public Region(long lID)
        {
            this.ID = lID;
        }
        public void Save(MySqlConnection con, MySqlTransaction trans)
        {
            string sqlCommand = "";
            if (ID <= 0) // new object, we insert new record to database
            {
                // build SQL statement
                sqlCommand = "Insert into tbl_Region (" +
                             "Name," +
                             "Code," +
                             "Sort" +
                             ") " +
                         " values(" +
                             "'" + DBModule.RefineString(Name) + "'," +
                             "'" + DBModule.RefineString(Code) + "'," +
                             Sort.ToString() +
                             ")";
                // run SQL statement
                DBModule.ExecuteNonQuery(sqlCommand, con, trans);
                sqlCommand = "select ID from tbl_Region order by ID desc limit 0,1";
                this.ID = long.Parse(DBModule.ExecuteQueryForOneResult(sqlCommand, con, trans));
                UserObjectsLog.SaveObjectActionLog("Region", "add", con, trans);
            }
            else // edit object, we update old record in database
            {
                // build SQL statement
                sqlCommand = "Update tbl_Region set " +
                             "ID=" + ID.ToString() + "," +
                             "Name='" + DBModule.RefineString(Name) + "'," +
                             "Code='" + DBModule.RefineString(Code) + "'," +
                             "Sort=" + Sort.ToString() +
                    " where ID=" + ID.ToString();
                // run SQL statement
                DBModule.ExecuteNonQuery(sqlCommand, con, trans);
                UserObjectsLog.SaveObjectActionLog("Region", "edit", con, trans);
            }
        }
        public void Delete(MySqlConnection con, MySqlTransaction trans)
        {
            //Buil SQL Statement
            //Run SQL Statement
            string sqlCommand = "DELETE FROM tbl_Region WHERE ID = " + this.ID.ToString() + " limit 1";
            DBModule.ExecuteNonQuery(sqlCommand, con, trans);
            UserObjectsLog.SaveObjectActionLog("Region", "delete", con, trans);
        }
        public void Load(MySqlConnection con, MySqlTransaction trans)
        {
            string sqlCommand = "";
            //Build SQL Statement
            //Run SQL Statement
            sqlCommand = "SELECT * FROM tbl_Region WHERE ID = " + ID.ToString() + " limit 0,1";
            DataSet ds = DBModule.ExecuteQuery(sqlCommand, con, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID"))
                    ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("Name"))
                    Name = dr["Name"].ToString();
                if (!dr.IsNull("Code"))
                    Code = dr["Code"].ToString();
                if (!dr.IsNull("Sort"))
                    Sort = int.Parse(dr["Sort"].ToString());
            }
            else
            {
            }
        }
        public void Load(string inputName, MySqlConnection con, MySqlTransaction trans)
        {
            string sqlCommand = "";
            //Build SQL Statement
            //Run SQL Statement
            sqlCommand = "SELECT * FROM tbl_Region WHERE Name = '" + DBModule.RefineString(inputName) + "' limit 0,1";
            DataSet ds = DBModule.ExecuteQuery(sqlCommand, con, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID"))
                    ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("Name"))
                    Name = dr["Name"].ToString();
                if (!dr.IsNull("Code"))
                    Code = dr["Code"].ToString();
                if (!dr.IsNull("Sort"))
                    Sort = int.Parse(dr["Sort"].ToString());
            }
            else
            {
            }
        }
        public static void GetList(string iDs, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection con, MySqlTransaction trans)
        {
            NumberOfPages = 0;
            string sqlCommand = "";
            ds = null;
            // build SQL statement
            sqlCommand = "Select * from tbl_Region where 1=1 ";
            if ((iDs != "") && (iDs != null))
            {
                sqlCommand += " AND `ID` in (" + iDs + ")";
            }

            if ((OrderBy != null) && (OrderBy != ""))
                sqlCommand = sqlCommand + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(sqlCommand, NumOfItemsPerPage, PageNo, out NumberOfPages, con, trans);
            }
            else
                ds = DBModule.ExecuteQuery(sqlCommand, con, trans);
        }
        public static void DoSearch(string strCondition, string OrderBy, out DataSet ds, MySqlConnection con, MySqlTransaction trans)
        {
            string sqlCommand = "";
            ds = null;
            // build SQL statement
            sqlCommand = "Select * from tbl_Region where 1=1 ";
            if (strCondition.Length > 0)
                sqlCommand += strCondition;
            if ((OrderBy != null) && (OrderBy != ""))
                sqlCommand = sqlCommand + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            ds = DBModule.ExecuteQuery(sqlCommand, con, trans);
        }
    }
}
